package com.bcx.crm.plat.core.utils;

import com.bcx.crm.plat.util.UtilTool;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * Created by ys on 2018/2/10.
 */
public class StringUtils {
    private static final Map<String,Object> pubMap = new HashMap<>();

    public static void main(String[] args) {
        pubMap.clear();
        pubMap.put("rowId","sssss");
        pubMap.put("state","edit");
        pubMap.put("templateCode","0sdfd");
        pubMap.put("templateName","name");
//        StringUtils.columnAddUnderline(pubMap);
        isAllLetter("SDFSDFSDFS");
    }

    /**
     * 将驼峰规则转换为下划线规则
     * @param map 传递参数的容器
     * @return 转换为下划线的map
     */
    public static Map<String, Object> columnAddUnderline(Map<String,Object> map){
        Set<String> strings = map.keySet();
        Map<String, Object> hashMap = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        for(String string:strings){
            stringBuilder.delete(0,stringBuilder.length());
            for(int i=0;i<string.length();i++){
                String character = string.substring(i, i + 1);
                if (character.toUpperCase().equals(character)){
                    stringBuilder.append("_").append(character.toLowerCase());
                    continue;
                }
                stringBuilder.append(character);
            }
            hashMap.put(stringBuilder.toString(),map.get(string));
        }
        return hashMap;
    }

    /**
     * 判断传递进来的字符串是否是英文
     * @param value 传递进来的字符串
     * @return 是否是英文字母
     */
    public static boolean isAllLetter(String value){
        if (!UtilTool.isValidString(value)){
            return false;
        }
        //boolean matches = value.matches("^[a-zA-Z]+$");
        Pattern compile = Pattern.compile("^[a-zA-Z]+$");
        return compile.matcher(value).matches();
    }
}
